function [Model, ArgCurrent, CurrentTrueList] = Ddavid_find_tightest_svdd(Data, TrueLabel, R)

% [Model, ArgCurrent, CurrentTrueList] = Ddavid_find_tightest_svdd(Data,
%   TrueLabel, R)
%
% <Input>
% Data: [n*m], n is the number of data instances, m is the number of
%              features
% TrueList: [n*1], {0, 1}, 1 means the points is considered as this label
% R: double, the relaxation parameter, R >= 0
%
% <Output>
% Model
% ArgCurrent: double, = Model.options.arg, the used value of arg of the
%                     tightest SVDD
% CurrentTrueList: [n*1], {0, 1}, 1 means the instance is inside the SVDD

disp(['Ddavid_find_tightest_svdd R = ' num2str(R)]);

ArgCurrent = 0.5;
ArgMin = 0.001;
C = 1;

InitTrueSize = sum(TrueLabel == 1);
AllowedSize = InitTrueSize * (1.0 + R);
disp(['  InitTrueSize = ' num2str(InitTrueSize) ' AllowedSize = ' num2str(AllowedSize)]);

% SVDD
[Model, AllDist] = Ddavid_svdd(Data, TrueLabel, ArgCurrent, C);
InsideSize = sum(AllDist <= Model.r);
disp(['  Arg = ' num2str(ArgCurrent) ' Inside = ' num2str(InsideSize)]);

% Loop SVDD to find the tightest Arg
while (InsideSize > AllowedSize && ArgCurrent > ArgMin)
    ArgCurrent = ArgCurrent / 2.0;
    [Model, AllDist] = Ddavid_svdd(Data, TrueLabel, ArgCurrent, C);
    
    InsideSize = sum(AllDist <= Model.r);

    disp(['  Arg = ' num2str(ArgCurrent) ' Inside = ' num2str(InsideSize)]);
end

CurrentTrueList = (AllDist <= Model.r);
